package flc.ast.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.CollectionUtils;
import com.blankj.utilcode.util.SPUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import flc.ast.Constant;
import flc.ast.R;
import flc.ast.adapter.BgmAdapter;
import flc.ast.adapter.FishAdapter;
import flc.ast.bean.BgmBean;
import flc.ast.bean.FishBean;
import flc.ast.databinding.FragmentStyleBinding;
import flc.ast.utils.AudioPlayer;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.AssetUtil;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link StyleFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class StyleFragment extends BaseNoModelFragment<FragmentStyleBinding> {

    private FishAdapter fishAdapter;
    private BgmAdapter bgmAdapter;
    private int selIcon, selBgm;
    private List<String> woodenFishList = new ArrayList<>();

    @Override
    protected int onCreate() {
        return R.layout.fragment_style;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.rvFishList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        fishAdapter = new FishAdapter();
        mDataBinding.rvFishList.setAdapter(fishAdapter);
        fishAdapter.setOnItemClickListener(this);

        mDataBinding.rvBgmList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        bgmAdapter = new BgmAdapter();
        mDataBinding.rvBgmList.setAdapter(bgmAdapter);
        bgmAdapter.setOnItemClickListener(this);

        mDataBinding.etFloatText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String str = s.toString();
                SPUtils.getInstance().put(Constant.FLOAT_CHAR_KEY, str);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (adapter == fishAdapter) {
            fishAdapter.getItem(selIcon).setSelected(false);
            selIcon = position;
            fishAdapter.getItem(position).setSelected(true);
            fishAdapter.notifyDataSetChanged();
            SPUtils.getInstance().put(Constant.WOODEN_FISH_KEY, position);
        } else if (adapter == bgmAdapter) {
            bgmAdapter.getItem(selBgm).setSelected(false);
            selBgm = position;
            bgmAdapter.getItem(position).setSelected(true);
            bgmAdapter.notifyDataSetChanged();
            SPUtils.getInstance().put(Constant.MUSIC_TONE_KEY, position);
            AudioPlayer.getInstance().startVoice(mContext, woodenFishList.get(position));
        }
    }

    @Override
    protected void initData() {
        String selLabel = SPUtils.getInstance().getString(Constant.FLOAT_CHAR_KEY, getString(R.string.merits_add_tips));
        mDataBinding.etFloatText.setText(selLabel);
        //木鱼皮肤
        List<FishBean> listFish = new ArrayList<>();
        listFish.add(new FishBean(R.drawable.aicon_my1, false));
        listFish.add(new FishBean(R.drawable.aicon_my2, false));
        listFish.add(new FishBean(R.drawable.aicon_my3, false));
        listFish.add(new FishBean(R.drawable.aicon_my4, false));
        listFish.add(new FishBean(R.drawable.aicon_my5, false));
        listFish.add(new FishBean(R.drawable.aicon_my6, false));
        listFish.add(new FishBean(R.drawable.aicon_my7, false));
        listFish.add(new FishBean(R.drawable.aicon_my8, false));
        selIcon = SPUtils.getInstance().getInt(Constant.WOODEN_FISH_KEY, 0);
        listFish.get(selIcon).setSelected(true);
        fishAdapter.setList(listFish);
        //背景音乐
        List<BgmBean> listBgm = new ArrayList<>();
        woodenFishList = AssetUtil.getFilePaths(mContext, "woodenFish");
        if (!CollectionUtils.isEmpty(woodenFishList)) {
            for (int i = 0; i < woodenFishList.size(); i++) {
                int num = i + 1;
                listBgm.add(new BgmBean(getString(R.string.bgm_name) + num, false));
            }
            selBgm = SPUtils.getInstance().getInt(Constant.MUSIC_TONE_KEY, 0);
            listBgm.get(selBgm).setSelected(true);
            bgmAdapter.setList(listBgm);
        }
    }
}